package org.liang.email4j;

import org.liang.common.property.PropertiesParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 邮件发送器工厂
 * 
 * @author L.Yang
 * @version v1.0 2015年12月29日 下午6:45:38
 */
public final class EmailSenderFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailSenderFactory.class);

    /** 默认配置文件名称. */
    private static final String VAL_DEF_PROP_FILE = "email4j.properties";

    /** 默认配置文件包路径. */
    private static final String VAL_DEF_PROP_PACKAGE = "com/tsingsoft/plugin/email4j";

    /** 默认配置文件包路径. */
    private static final String VAL_DEF_SENDER_CLASS =
                    "com.tsingsoft.plugin.email4j.jodd.JoddmailSender";

    /**
     * 根据发送器名称生成发送器.
     * 
     * @param name
     *            submail or jodd
     * @return 发送器
     */
    public static EmailSender getSender(String name) {
        EmailSender sender = null;
        try {
            PropertiesParser prop = new PropertiesParser(VAL_DEF_PROP_FILE, VAL_DEF_PROP_PACKAGE);
            Class<?> clazz = Class.forName(prop.getString(name, VAL_DEF_SENDER_CLASS));
            sender = (EmailSender) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            LOGGER.error(e.getMessage());
        } catch (InstantiationException e) {
            LOGGER.error(e.getMessage());
        } catch (IllegalAccessException e) {
            LOGGER.error(e.getMessage());
        }
        return sender;
    }
}
